<template>
  <div>
    <!--  面包屑-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--    卡片-->
    <el-card>
      <!--      第一行的输入搜索，按钮-->
      <el-row :gutter="15">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable @clear="headSearchClosed" v-model="headSearch">
            <el-button slot="append" icon="el-icon-search" @click="searchFn"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddGood">添加商品</el-button>
        </el-col>
      </el-row>
      <!--      表格-->
      <el-table :data="goodList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="120"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column label="创建时间" width="200">
          <template #default="scope">
            {{scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" class="el-icon-edit" size="small"></el-button>
            <el-button type="danger" class="el-icon-delete" @click="deleteRowdata(scope.row)" size="small"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--      分页-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[10, 30, 50, 100]"
        :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'List',
  data () {
    return {
      // 获取商品列表的参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 商品列表
      goodList: [],
      // 商品总数
      total: 0,
      // 头部搜索数据绑定
      headSearch: ''
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败')
      this.$message.success('成功获取商品列表')
      // console.log(res)
      this.goodList = res.data.goods
      this.total = res.data.total
    },
    // 分页：修改页面显示数量大小
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 分页：修改当前页面页码
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 点击搜索触发的事件
    searchFn () {
      this.queryInfo.query = this.headSearch
      this.getGoodsList()
    },
    // 头部搜索清空时，获取的数据
    headSearchClosed () {
      this.queryInfo.query = ''
      this.getGoodsList()
    },
    // 删除商品
    async deleteRowdata (row) {
      const confirmReslut = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmReslut !== 'confirm') return this.$message.info('取消删除')
      const { data: res } = await this.$http.delete('goods/' + row.goods_id)
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getGoodsList()
    },
    // 点击添加按钮跳转页面
    goAddGood () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.el-table{
  font-size: 13px;
}
</style>
